package com.glory.study.controller;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.glory.study.model.GroupIdInfo;
import com.glory.study.model.PageSearch;
import com.glory.study.service.GroupIdInfoService;
import com.glory.study.service.PageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListTestController {

    @Autowired
    private GroupIdInfoService groupIdInfoService;

    @Autowired
    PageService pageService;

    @RequestMapping(value = "/pagelist", method = {RequestMethod.GET, RequestMethod.POST})
    public String pagelist() {

        return "pagelist";
    }

    @RequestMapping("/pagelsitquery")
    @ResponseBody
    public String pagelsitquery(HttpServletResponse response, ModelMap modelMap, PageSearch pageConf) throws IOException {


        int currentPage = pageConf.getCurrentPage();
        int pageSize = pageConf.getPageSize();
        int pagestart=(currentPage-1)*pageSize;
        String tables = "groupidinfo t   ";
        String files = "t.*";
        String order = "order by t.id desc ";
        String where = "1=1";

        if(!StringUtils.isBlank(pageConf.getKey())){
            String key=pageConf.getKey();
            where=where+" and  groupId like '%"+key+"%' ";
        }


        int total=pageService.findCount(tables,where);
        List<HashMap<String, Object>> map= pageService.findMapPage(tables,files,order,where,pagestart,pageSize);
        Map<String, Object> obj=new HashMap<String,Object>();
        obj.put("total",total);
        obj.put("list",map);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString=objectMapper.writeValueAsString(obj);

        return  jsonString;

    }





}
